package com.zsrt.oa.recharge.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.zsrt.oa.recharge.dao.RechargeDao;
import com.zsrt.oa.recharge.dao.impl.RechargeDaoImpl;
import com.zsrt.oa.recharge.entity.RechargeEntity;
import com.zsrt.oa.recharge.service.RechargeService;
import com.zsrt.oa.user.dao.UserDao;
import com.zsrt.oa.user.dao.impl.UserDaoImpl;
import com.zsrt.oa.user.entity.UserEntity;

public class RechargeServiceImpl implements RechargeService{
	
	/**
	 * 初始化充值表的持久层
	 */
	private RechargeDao dao = new RechargeDaoImpl();
	
	/**
	 * 初始化用户的持久层
	 */
	private UserDao userDao = new UserDaoImpl();
	
	/**
	 * 增加一条充值记录
	 * @param userId
	 * @param rechargeMoney
	 * @param map
	 */
	@Override
	public void rechargeAdd(Object phone, String rechargeMoney, Map<String, Object> map) {
		RechargeEntity entity = new RechargeEntity();
		String userPhone = (String) phone;
		entity.setUserId(userPhone);
		entity.setRechargeMoney(Double.valueOf(rechargeMoney));
		try {
			int result = dao.rechargeAdd(entity);
			
			//充值成才对用户表进行修改
			if(result == 1){
				UserEntity userEntity = new UserEntity();
				userEntity.setUserBalance(Double.valueOf(rechargeMoney));
				userEntity.setUserPhone(userPhone);
				userDao.updateUserMoney(userEntity);
				
				String levelName = userDao.returnUserLevel(userPhone);
				userEntity.setUserLevel(levelName);
				int counter = userDao.updateUserLevel(userEntity);
				if(counter == 1){
					map.put("result", "success");
					map.put("message", "充值成功..");
				}else{
					map.put("result", "failure");
					map.put("message", "充值失败..");
				}
				
			}else{
				map.put("result", "failure");
				map.put("message", "充值失败..");
			}
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询充值记录
	 * @param userPhone 需要查询的用户手机
	 * @param map
	 */
	@Override
	public void rechargeQuery(String userPhone, Map<String, Object> map) {
		RechargeEntity entity = new RechargeEntity();
		entity.setUserId(userPhone);
		try {
			List<RechargeEntity> list = dao.rechargeQuery(entity);
			map.put("result", "success");
			map.put("message", "查询成功..");
			map.put("data", list);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
}
